package com.isnoam.calendar.view

import android.content.Context
import android.util.AttributeSet
import com.isnoam.calendar.base.BaseCalendarView
import com.isnoam.calendar.base.BaseMonthView
import com.isnoam.calendar.bean.DateInfo
import com.isnoam.calendar.bean.ViewAttrs
import com.isnoam.calendar.listener.OnDateSelectedListener
import com.isnoam.calendar.listener.OnMultiDateSelectedListener
import com.isnoam.calendar.view.month.MultiMonthView
import java.util.*

/**
 * 多选择日期控件
 */
class MultiCalendarView(
    context: Context, attrs: AttributeSet
) : BaseCalendarView(context, attrs) {

    private var listener: OnMultiDateSelectedListener? = null

    // 存放所有选中的日期
    var selectedDateList = mutableListOf<DateInfo>()
        set(value) {
            selectedDateList.addAll(value)
        }

    /**
     * 创建月份
     */
    override fun createMonthView(
        position: Int, currentMonth: Calendar, viewAttrs: ViewAttrs
    ): BaseMonthView {
        val monthView = MultiMonthView(context, currentMonth, position, viewAttrs, selectedDateList)
        monthView.onDateSelectedListener = object : OnDateSelectedListener {
            override fun onDateSelected(
                dateInfo: DateInfo, changeMonth: Boolean, monthPosition: Int
            ) {
                updateDateSelected(dateInfo, changeMonth, monthPosition)
                listener?.let { it(this@MultiCalendarView, dateInfo, selectedDateList) }
            }
        }
        return monthView
    }

    fun setOnMultiDateSelectedListener(listener: OnMultiDateSelectedListener) {
        this.listener = listener
    }
}